import win32api
import win32con
from time import sleep
class MouseController:
    def __init__(self,p_x=0,p_y=0):
        self.p_x = p_x
        self.p_y = p_y
    @staticmethod
    def move(dx, dy):
        """
        移动鼠标相对位置 (dx, dy)
        :param dx: 水平移动距离
        :param dy: 垂直移动距离
        """
        #先将dx和dy除以2转成整数
        dx = int(dx / 2)
        dy = int(dy / 2)
        win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, dx, dy, 0, 0)

    def left_click(self,x=None, y=None,delay=0.05,double_click=False,offset=False):
        """
        左键点击指定坐标 (x, y)
        :param x: 水平坐标
        :param y: 垂直坐标
        """
        if not x is None or not y is None:
            if offset:
                x = x + self.p_x
                y = y + self.p_y
            win32api.SetCursorPos((x, y))
        
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        sleep(delay)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

        if double_click:
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
            sleep(delay)
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        

    def right_click(self,x=None, y=None,delay=0.05,double_click=False):
        """
        右键点击指定坐标 (x, y)
        :param x: 水平坐标
        :param y: 垂直坐标
        """
        if not x is None or not y is None:
            win32api.SetCursorPos((x, y))
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
        sleep(delay)
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
        if double_click:
            win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
            sleep(delay)
            win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)

    @staticmethod
    def left_down(x=None, y=None,delay=0.05):
        """
        左键按下指定坐标 (x, y)
        :param x: 水平坐标
        :param y: 垂直坐标
        """
        if not x is None or not y is None:
            win32api.SetCursorPos((x, y))
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        sleep(delay)
    
    @staticmethod
    def left_up(x=None, y=None,delay=0.05):
        """
        左键松开指定坐标 (x, y)
        :param x: 水平坐标
        :param y: 垂直坐标
        """
        if not x is None or not y is None:
            win32api.SetCursorPos((x, y))
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        sleep(delay)
    
    @staticmethod
    def right_down(x=None, y=None,delay=0.05):
        """
        右键按下指定坐标 (x, y)
        :param x: 水平坐标
        :param y: 垂直坐标
        """ 
        if not x is None or not y is None:
            win32api.SetCursorPos((x, y))
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
        sleep(delay)

    @staticmethod
    def right_up(x=None, y=None,delay=0.05):
        """
        右键松开指定坐标 (x, y)
        :param x: 水平坐标
        :param y: 垂直坐标
        """
        if not x is None or not y is None:
            win32api.SetCursorPos((x, y))
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
        sleep(delay)









if __name__ == "__main__":
    mouse = MouseController()
    mouse.move(0,3000)
    sleep(1)
    mouse.move(0,-1200)